<?php
namespace App\Http\Controllers\Api;

use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\Utils;
use App\Models\WxQuestion;
use Illuminate\Http\Request;

class HelpController extends BaseController
{
    public function detail(Request $request){
        $help_id = $request->id;
        if(_empty_($help_id)){
            return $this->fail(200001);
        }
        $help = WxQuestion::find($help_id);
        if($help){
            return $this->success($help);
        }else{
            return $this->fail(200003);
        }
    }
    /*
     * 获取热门问题列表
     */
    public function questions(Request $request){
        $types = $request->types;
        if(!is_array($types)){
            $types = json_decode($types, true);
        }
        if(_empty_($types)){
            return $this->fail(200001, [], 'types参数为空');
        }
        $questions = WxQuestion::whereIn('type', $types)->orderBy('updated_at', 'desc')->limit(200)->get();
        if(_empty_($questions)){
            return $this->fail(200003);
        }

        $res = [];
        $nameMap = FieldUtils::getQuestionTypes();
        foreach ($types as $type){
            $res[$type] = ['name'=>$nameMap[$type], 'list'=>[]];
        }
        $questions = $questions->map(function ($v) use (&$res, $nameMap){
            if(count($res[$v->type]) <= 5){
                $v->typeName = $nameMap[$v->type];
                $res[$v->type]['list'][] = $v;
            }
            return $res;
        });
        if(_empty_($res)){
            return $this->fail(200003);
        }
        return $this->success(array_values($res));

    }
}
